const path = require('path')
const fs = require('fs')


const tables = {}

const jsonPath = path.resolve(__dirname, "formats.json")

class FormatHandler {

    static add (ext, signature, desc = "") {
        ext = ext.trim().toLocaleLowerCase()
        signature = signature.replace(/\s+/g, "")
        desc = desc.trim()
        
        tables[signature] = {
            desc,
            dec: parseInt(signature, 16),
            ext
        }
        return this
    }

    static get tables () {
        return tables
    }

    static write () {
        const data = JSON.stringify(tables)
        fs.writeFileSync(jsonPath, data, {
            flag: "w"
        })
    }

    static setOffset(sign,offset){
        tables[sign.replace(/\s+/g, "")].offset = offset
        return this
    }

}

exports.FormatHandler = FormatHandler